﻿using MediaImporter.Controllers;
using MediaImporter.Controls;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MediaImporter
{
    public partial class MainView : Form, IMainView
    {

        ImportController importController;
        BackgroundCopyController rawCopyController;

        public MainView()
        {
            InitializeComponent();
            rawCopyController = new BackgroundCopyController(progressControl1);
            rawCopyController.FileCopied += copyController_FileCopied;
            rawCopyController.Done += FilesCopied;
            importController = new ImportController(this);
            importController.Done += FilesCopied;
            settingsPanel1.ImportPressed += settingsPanel1_ImportPressed;
        }

        void settingsPanel1_ImportPressed(object sender, EventArgs e)
        {
            importController.StartImport();
        }

        void FilesCopied(object sender, RunWorkerCompletedEventArgs e)
        {
            panelActions.Enabled = true;
            if (!e.Cancelled && e.Error == null)
                MessageBox.Show("Done");

        }
        public ProgressControl ProgressControl
        {
            get { return progressControl1; }
        }

        void copyController_FileCopied(object sender, FileCopiedEventArgs e)
        {
            matchFiles1.MarkAsDone(e.Name);
        }

        private void buttonShowResults_Click(object sender, EventArgs e)
        {
            if (!PathSelected())
                return;

            try
            {
                var result = GetFilesList(DestinationFolder, SourceFolder);
                matchFiles1.Files = result;
                matchFiles1.RefreshResults();
                panelActions.Enabled = result.Count > 0;
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private NameValueCollection GetFilesList(string jpegPath, string rawPath)
        {
            var jpegs = Directory.GetFiles(jpegPath, "*.jpg").Select(filepath => Path.GetFileNameWithoutExtension(filepath)).ToArray();
            var raws = Directory.GetFiles(rawPath, "*.cr2").ToArray();

            var result = new NameValueCollection();
            foreach (var raw in raws)
            {
                string rawName = Path.GetFileNameWithoutExtension(raw);
                string jpegName = jpegs.FirstOrDefault(name => string.Compare(name, rawName, true) == 0);
                result[raw] = jpegName;
            }
            return result;
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {

            if (!PathSelected() || matchFiles1.Files == null)
                return;
            panelActions.Enabled = false;
            rawCopyController.StartCopyFiles(DestinationFolder + "-RAW", matchFiles1.Files.AllKeys.Where(file => matchFiles1.Files[file] != null), "Copying matching RAWs");
        }

        private bool PathSelected()
        {
            return !string.IsNullOrEmpty(DestinationFolder) && !string.IsNullOrEmpty(SourceFolder);
        }

        public string DestinationFolder
        {
            get { return settingsPanel1.DestinationFolder; }
        }

        public string SourceFolder
        {
            get { return settingsPanel1.SourceFolder; }
        }
    }
}
